package com.example.bookkeeping1220;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.example.bookkeeping1220.R;
import com.example.bookkeeping1220.adapter.AccountAdapter;
import com.example.bookkeeping1220.adapter.AchievementAdapter;
import com.example.bookkeeping1220.db.AccountBean;
import com.example.bookkeeping1220.db.AchievementBean;
import com.example.bookkeeping1220.db.DBManager;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

public class AchievementActivity extends AppCompatActivity {
    private GridView achieveGv;
    AchievementAdapter achievementAdapter;
    AccountAdapter accountAdapter;
    List<AchievementBean> achieveDatas;  //成就的bean
    AchievementInfo[] achievementInfos; //保存上述成就对应的各项信息
    int achieveNum;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_achievement);
        achieveGv = findViewById(R.id.achieve_show);
        buildAchieveDatas();
        achievementAdapter = new AchievementAdapter(AchievementActivity.this,achieveDatas);
        achieveGv.setAdapter(achievementAdapter);
        setGVLongClickListener();
    }

    /*查询对应类型对应的金额，kind为1表示收入*/
    private static float requireDBdata(String typename, int kind) {
        Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        float sumMoney = DBManager.getSumMoneyOneYearInOneTypename(year,kind,typename);
        return sumMoney;
    }

    /*点击事件*/
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.achieve_iv_back:
                finish();
                break;
        }
    }

    /** 设置GridView的点击事件*/
    private void setGVLongClickListener() {
        achieveGv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                AchievementBean clickBean = achieveDatas.get(i);  //获取正在被点击的这条信息

                //弹出提示用户是否删除的对话框
                showItemDialog(clickBean);
            }
        });
    }
    private void showItemDialog(AchievementBean clickBean) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        AchievementInfo clickAchievementInfo = achievementInfos[clickBean.getId()];
        float nowCost = requireDBdata(clickAchievementInfo.initialAchieveTypename, clickAchievementInfo.initialAchieveKind);

        /*分支决定输出提示信息*/
        if(clickAchievementInfo.initialAchieveTypename == "餐饮")
            if (nowCost < clickAchievementInfo.initialAchieveTargetMoney) {
                String message = "因为您吃得不够多,您餐饮消费须达到:" + clickAchievementInfo.initialAchieveTargetMoney + ",您目前消费为:" + nowCost;
                builder.setMessage(message)
                        .setPositiveButton("确定", null);
                builder.create().show();   //显示对话框
            } else {
                builder.setMessage("因为您吃够多了，称得上是" + clickBean.getTypename())
                        .setPositiveButton("确定", null);
                builder.create().show();   //显示对话框
            }

        if(clickAchievementInfo.initialAchieveTypename == "购物")
            if (nowCost < clickAchievementInfo.initialAchieveTargetMoney) {
                String message = "因为您买得不够多,您购物消费须达到:" + clickAchievementInfo.initialAchieveTargetMoney + ",您目前消费为:" + nowCost;
                builder.setMessage(message)
                        .setPositiveButton("确定", null);
                builder.create().show();   //显示对话框
            } else {
                builder.setMessage("达成成就：" + clickBean.getTypename())
                        .setPositiveButton("确定", null);
                builder.create().show();   //显示对话框
            }

        if(clickAchievementInfo.initialAchieveTypename == "奖金")
            if (nowCost < clickAchievementInfo.initialAchieveTargetMoney) {
                String message = "因为您赚得不够多,您奖金收入须达到:" + clickAchievementInfo.initialAchieveTargetMoney + ",您目前消费为:" + nowCost;
                builder.setMessage(message)
                        .setPositiveButton("确定", null);
                builder.create().show();   //显示对话框
            } else {
                builder.setMessage("太能赚了。。达成成就" + clickBean.getTypename())
                        .setPositiveButton("确定", null);
                builder.create().show();   //显示对话框
            }
    }


    /*成就不多，简单加载就好*/
    private void buildAchieveDatas() {
        achieveDatas = new ArrayList<>();
        achievementInfos = new AchievementInfo[9];
        /*餐饮上，分支决定加载的颜色*/
        if(requireDBdata("餐饮",0) > 1000)
            achievementInfos[0] = new AchievementInfo(new AchievementBean(0,"小吃货",R.drawable.cup3),"餐饮",1000,0);
        else
            achievementInfos[0] = new AchievementInfo(new AchievementBean(0,"小吃货",R.drawable.cup1_fs),"餐饮",1000,0);
        if(requireDBdata("餐饮",0) > 10000)
            achievementInfos[1] = new AchievementInfo(new AchievementBean(1,"干饭王",R.drawable.cup2),"餐饮",10000,0);
        else
            achievementInfos[1] = new AchievementInfo(new AchievementBean(1,"干饭王",R.drawable.cup1_fs),"餐饮",10000,0);
        if(requireDBdata("餐饮",0) > 20000)
            achievementInfos[2] = new AchievementInfo(new AchievementBean(2,"食神",R.drawable.cup1),"餐饮",20000,0);
        else
            achievementInfos[2] = new AchievementInfo(new AchievementBean(2,"食神",R.drawable.cup1_fs),"餐饮",20000,0);


        if(requireDBdata("购物",0) > 1000)
            achievementInfos[3] = new AchievementInfo(new AchievementBean(3,"买买买",R.drawable.cup3),"购物",1000,0);
        else
            achievementInfos[3] = new AchievementInfo(new AchievementBean(3,"买买买",R.drawable.cup1_fs),"购物",1000,0);
        if(requireDBdata("购物",0) > 10000)
            achievementInfos[4] = new AchievementInfo(new AchievementBean(4,"还能买",R.drawable.cup2),"购物",10000,0);
        else
            achievementInfos[4] = new AchievementInfo(new AchievementBean(4,"还能买",R.drawable.cup1_fs),"购物",10000,0);
        if(requireDBdata("购物",0) > 20000)
            achievementInfos[5] = new AchievementInfo(new AchievementBean(5,"太能买",R.drawable.cup1),"购物",20000,0);
        else
            achievementInfos[5] = new AchievementInfo(new AchievementBean(5,"太能买",R.drawable.cup1_fs),"购物",20000,0);


        if(requireDBdata("奖金",1) > 1000)
            achievementInfos[6] = new AchievementInfo(new AchievementBean(6,"初拿奖金",R.drawable.cup3),"奖金",1000,1);
        else
            achievementInfos[6] = new AchievementInfo(new AchievementBean(6,"初拿奖金",R.drawable.cup1_fs),"奖金",1000,1);
        if(requireDBdata("奖金",1) > 10000)
            achievementInfos[7] = new AchievementInfo(new AchievementBean(7,"奖金成山",R.drawable.cup2),"奖金",10000,1);
        else
            achievementInfos[7] = new AchievementInfo(new AchievementBean(7,"奖金成山",R.drawable.cup1_fs),"奖金",10000,1);
        if(requireDBdata("奖金",1) > 200000)
            achievementInfos[8] = new AchievementInfo(new AchievementBean(8,"奖金当工资",R.drawable.cup1),"奖金",200000,1);
        else
            achievementInfos[8] = new AchievementInfo(new AchievementBean(8,"奖金当工资",R.drawable.cup1_fs),"奖金",200000,1);

        for(int i=0;i<9;i++)
            achieveDatas.add(achievementInfos[i].initialAchieveData);
    }

    public static int getAchievementNum(){
        int achieveNum = 0;
        if(requireDBdata("餐饮",0) >= 20000)
            achieveNum+=3;
        else if(requireDBdata("餐饮",0) >= 10000)
            achieveNum+=2;
        else if(requireDBdata("餐饮",0) >= 1000)
            achieveNum+=1;
        if(requireDBdata("购物",0) >= 20000)
            achieveNum+=3;
        else if(requireDBdata("购物",0) >= 10000)
            achieveNum+=2;
        else if(requireDBdata("购物",0) >= 1000)
            achieveNum+=1;
        if(requireDBdata("奖金",1) >= 20000)
            achieveNum+=3;
        else if(requireDBdata("奖金",1) >= 10000)
            achieveNum+=2;
        else if(requireDBdata("奖金",1) >= 1000)
            achieveNum+=1;
        return achieveNum;
    }

    class AchievementInfo{
        AchievementBean initialAchieveData;
        String initialAchieveTypename;
        float initialAchieveTargetMoney;
        int initialAchieveKind;

        public AchievementInfo(AchievementBean initialAchieveData, String initialAchieveTypename, float initialAchieveTargetMoney, int initialAchieveKind) {
            this.initialAchieveData = initialAchieveData;
            this.initialAchieveTypename = initialAchieveTypename;
            this.initialAchieveTargetMoney = initialAchieveTargetMoney;
            this.initialAchieveKind = initialAchieveKind;
        }
    }
}
